using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

namespace Main
{
    public class Friend
    {
        public Friend()
        {

        }
        
#region "Properties"
        private int _ID;
        public int ID
        {
            get
            { return _ID;}
            set
            { _ID = value; }
        }

        private Guid _FriendID;
        public Guid FriendID
        {
            get
            { return _FriendID; }
            set
            { _FriendID = value; }
        }

        private Guid _UserID;
        public Guid UserID
        {
            get
            { return _UserID; }
            set
            { _UserID = value; }
        }
#endregion

        #region "Data Access Methods"
        public void Commit()
        {
            try
            {
                DataTable dt = DataAccess.CreateFriendship(UserID, FriendID);
                ID = (int)dt.Rows[0]["FriendshipID"];
            }
            catch (Exception e)
            {
                //Do something with E
            }
        }
        #endregion
        #region "Friend Specific Methods"
        //This could be changed to return the integer of the friendship
        //allowing deletion of said friendship.
        public static Boolean DoesFriendShipExist(Guid UserID, Guid FriendID)
        {
            DataTable dt = DataAccess.GetFriendShip(UserID, FriendID);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }


    public class FriendCollection : CollectionBase
    {
        public void Add(Friend currentFriend)
        {
            InnerList.Add(currentFriend);
        }

        public void Remove(int friendIndex)
        {
            InnerList.RemoveAt(friendIndex);
        }

        public Friend this[int index]
        {
            get
            { return (Friend)InnerList[index]; }
        }

        public void FillByUserID(Guid userID)
        {
            try
            {
                DataTable dt = DataAccess.GetFriendsByUserID(userID);
               
                foreach (DataRow row in dt.Rows)
                {
                    Friend newFriend = new Friend();
                    newFriend.FriendID = (Guid)row[dt.Columns.IndexOf("FriendID")];
                    newFriend.UserID = (Guid)row[dt.Columns.IndexOf("UserID")];
                    newFriend.ID = (int)row[dt.Columns.IndexOf("FriendshipID")];
                    InnerList.Add(newFriend);
                }
            }
            catch (Exception e)
            {
                //Probably should do something with e here.
                Console.Write(e);
            }
        }
    }
}
